/*******************************************************************
* MediaCoder - The universal audio/video transcoder
* Distributed under GPL license
* Copyright (c) 2005 Stanley Huang <stanleyhuangyc@yahoo.com.cn>
* All rights reserved.
*******************************************************************/

#include "processwrapper.h"

typedef int (*PFN_MP_SEND_CMD)(char* cmd);
typedef void (*PFN_MP_EXIT)(char* how);

#define CONSOLE_BUF_SIZE 2048

class CMPlayer;

typedef int (*PLAYER_EVENT_HANDLER)(int eventid, void* arg);

typedef enum {
	PC_CONSOLE_OUTPUT,
	PC_STATE_CHANGE,
} PLAYER_CALLBACKS;

typedef enum {
	PLAYER_IDLE = 0,
	PLAYER_PLAYING,
	PLAYER_PAUSED,	
	PLAYER_STOPPING,		// palyer going to be stopped
}  PLAYER_STATES;

typedef enum {
	PLAYER_POS = 0,
	PLAYER_STATE,
	PLAYER_PERCENTAGE,
	PLAYER_DURATION,
	PLAYER_HAS_AUDIO,
	PLAYER_HAS_VIDEO,
} PLAYER_INFO;

typedef struct {
	int id;
	int format;
	int bitrate;					// audio bit rate
	int samplerate;					// sampling rate
	int channels;					// number of channels
	int samplebits;					// bits of samples
	char codec[32];					// audio codec name string
} PLAYER_ATTR_AUDIO;

typedef struct {
	int id;
	int format;
	int bitrate;					// video bit rate
	int width;						// video width in pixels
	int height;						// video height in pixels
	float fps;						// video frame rate
	float aspect;					// video aspect ratio
	char codec[32];					// video codec name string
} PLAYER_ATTR_VIDEO;

class CMPlayer
{
public:
	CMPlayer(char* path = "plugins\\mplayer.exe");
	~CMPlayer();
	bool Play(char* filename, HWND hWndParent = 0, char* options = 0);
	void Stop();
	int Pause();
	int Step();
	bool GetFS();
	void SetFS(bool fs = true);
	bool Seek(int seekpos, int type = 2);
	bool Command(char* cmd, char** result = 0);
	int Read(char** pbuf, int bufsize);
	int GetPos();
	int GetPosData();
	int WaitEvent(int timeout);
	int DoCallback(int eventid, void* arg);
	void SetState(PLAYER_STATES newstate);
	void RefreshVideo();
	void ParseInfo(char* id, char* v);
	CProcessWrapper proc;
	PLAYER_STATES state;
	char* mppath;
	char* playingfile;
	PLAYER_EVENT_HANDLER callback;
	char* console;
	int pos;
	int duration;
	bool hasVideo;
	bool hasAudio;
	PLAYER_ATTR_VIDEO video;
	PLAYER_ATTR_AUDIO audio;
	HWND hWnd;
private:
	static void WaitLaunchDone(void* ptrme);
	bool onClose();
	static BOOL CALLBACK EnumWindowCallBack(HWND hwnd, LPARAM lParam);
	HANDLE hThread;
	HANDLE hMutexQuery;
	HANDLE hEventState;
};
